package com.hancheng.commoncloud.framework.util;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 */
public class JsonUtils {
    /**
     * Null serialize is used because else Gson will ignore all null fields.
     */
    private static Gson gson = new GsonBuilder()
            .disableHtmlEscaping()
            // 不能修改时间格式，会影响到其他服务
            // .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .serializeNulls().create();

    private static Gson gson2 = new GsonBuilder()
            .disableHtmlEscaping()
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .serializeNulls().create();
    /**
     * Made private because all methods are static and hence do not need
     * object instantiation
     */
    private JsonUtils() {
    }

    /**
     * To Json Converter using Goolge's Gson Package
     * <p>
     * this method converts a simple object to a json string
     *
     * @param obj
     * @return a json string
     */
    public static <T> String toJsonObj(T obj) {
        return gson.toJson(obj);
    }
    public static <T> String toJsonObj2(T obj) {
        return gson2.toJson(obj);
    }

    /**
     * Converts a collection of objects using Google's Gson Package
     *
     * @param objCol
     * @return a json string array
     */
    public static <T> String toJsonList(List<T> objCol) {
        return gson.toJson(objCol);
    }

    /**
     * Returns the specific object given the Json String
     *
     * @param <T>
     * @param jsonString
     * @param obj
     * @return a specific object as defined by the user calling the method
     */
    public static <T> T fromJsonToObj(String jsonString, Class<T> obj) {
        return gson.fromJson(jsonString, obj);
    }

    public static <T> T fromJsonToObj(String jsonString, Type t) {
        return gson.fromJson(jsonString, t);
    }

    /**
     * Returns a list of specified object from the given json array
     *
     * @param <T>
     * @param jsonString
     * @param t          the type defined by the user
     * @return a list of specified objects as given in the json array
     */
    public static <T> List<T> fromJsonToList(String jsonString, Type t) {
        return gson.fromJson(jsonString, t);
    }

    public static <K, V> Map<K, V> jsonToMap(String json) {
        return gson.fromJson(json,
                new TypeToken<Map<K, V>>() {
                }.getType());
    }

    public static JsonObject parseJsonObject(String jsonstr) {
        return new JsonParser().parse(jsonstr).getAsJsonObject();
    }

    public static boolean isJsonStrValidate(String jsonStr) {
        JsonElement jsonElement;
        try {
            jsonElement = new JsonParser().parse(jsonStr);
        } catch (Exception e) {
            return false;
        }
        if (jsonElement == null) {
            return false;
        }
        if (!jsonElement.isJsonObject()) {
            return false;
        }
        return true;
    }

}
